﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace MyPlatForms.Ctrls
{
    public partial class Theader_QrCode : UserControl
    {
        private string _mQrContent = "801000.19090910-0001.50000.TY63002";
        private string mPicString = "yu3zx.com";
        private Font _fontsize = new Font(new FontFamily("宋体"),12);
        private float fontweight = 12;//18
        public Theader_QrCode()
        {
            InitializeComponent();
            SetPic();
        }

        /// <summary>
        /// 设置中间显示内容
        /// </summary>
        public string QrContent
        {
            set
            {
                _mQrContent = value;
                if (_mQrContent.Length > 10)
                {
                    _mQrContent = _mQrContent.Substring(0, 10);
                }
                lblContent.Text = _mQrContent;
            }
        }

        public Font QrFontSize
        {
            set
            {
                _fontsize = value;
                lblContent.Font = _fontsize;
            }
            get { return _fontsize; }
        }

        /// <summary>
        /// 字体大小
        /// </summary>
        public float FontSize
        {
            get { return fontweight; }
            set
            {
                fontweight = value;
                lblContent.Font = new Font(new FontFamily("宋体"), fontweight);
            }
        } 

        /// <summary>
        /// 设置二维码
        /// </summary>
        public string SetPicString
        {
            set
            {
                mPicString = value;
                SetPic();
            }
            get
            {
                return mPicString;
            }
        }

        public void SetPic()
        {
            QrImgCtrl.Image = ThoughtWorks_QRCode(mPicString);
        }

        public void SetMode(PictureBoxSizeMode picMode)
        {
            QrImgCtrl.SizeMode = picMode;
        }

        private Bitmap ThoughtWorks_QRCode(string enCodeString)
        {
            System.Drawing.Bitmap bt;

            ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意：BYTE能支持中文，ALPHA_NUMERIC扫描出来的都是数字)
            qrCodeEncoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
            qrCodeEncoder.QRCodeVersion = 0;//版本(注意：设置为0主要是防止编码的字符串太长时发生错误)
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;//错误效验、错误更正(有4个等级)
            qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色
            qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色

            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);

            return bt;
        }

        private void QrImgCtrl_Resize(object sender, EventArgs e)
        {
            QrImgCtrl.Height = QrImgCtrl.Width;
            //lblContent.Top = this.Height - lblContent.Height - 10;// lblContent.Bottom
        }
    }
}
